package com.shangyueshang.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/24 19:07
 * @version: v1.0.0
 *
 * <p>入参保留两位小数</p>
 */
public class SerializerBigDecimal extends JsonSerializer<BigDecimal> {

    @Override
    public void serialize(BigDecimal value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (Objects.isNull(value)) {
            jsonGenerator.writeNull();
        } else {
            // 这里取floor
           // jsonGenerator.writeNumber(value.setScale(2, RoundingMode.FLOOR))
            jsonGenerator.writeNumber(value.setScale(2));
        }

    }
}
